 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : EasyAvatar-Android
 * Package  : net.solosky.easyavatar.utils
 * File     : MatchHelper.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : Nov 13, 2011
 * License  : Apache License 2.0 
 */
package net.solosky.easyavatar.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.solosky.easyavatar.api.Friend;
import net.solosky.easyavatar.api.FriendList;
import net.solosky.easyavatar.api.Group;
import net.solosky.easyavatar.bean.Contact;
import net.solosky.easyavatar.bean.Match;
import net.solosky.easyavatar.bean.Match.MatchType;
import android.graphics.Bitmap;

/**
 *
 * 工具类，处理好友匹配逻辑
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class MatchHelper {
	/**保存了所有好友列表，无论是否有联系人匹配成功，GID=>Matches集合*/
	private Map<String, List<Match>> mMatchListMap;
	/**分组列表*/
	private List<Group> mGroupList;
	/**是否还有数据*/
	private boolean mMoreFriend;
	/**下一次获取数据的参数*/
	private String   mNextIndex;
	/**默认分组标题*/
	private String   mDefaultGrouTitle;
	
	public MatchHelper(){
		mMatchListMap = new HashMap<String, List<Match>>();
		mGroupList = new ArrayList<Group>();
		mDefaultGrouTitle = "默认分组";
		mMoreFriend = true;
		mNextIndex = null;
	}
	
	
	/**
	 * 判断是否有数据
	 * @return
	 */
	public boolean hasFriend(){
		return mMatchListMap.size()>0;
	}
	
	/**
	 * 是否还有好友数据
	 * @return
	 */
	public boolean hasNext(){
		return mMoreFriend;
	}
	
	/**
	 * 获取下一次获取好友的位置
	 * @return
	 */
	public String nextIndex(){
		return mNextIndex;
	}
	
	/**
	 * 设置默认分组的标题
	 * @param title
	 */
	public void setDefaultGroupTitle(String title){
		mDefaultGrouTitle = title;
	}
	
	/**
	 * 把获取的好友列表附加到匹配列表中
	 * @param friendList
	 */
	public void appendFriendList(FriendList friendList){
		
		//状态信息
		mMoreFriend = friendList.hasNext();
		mNextIndex  = friendList.nextIndex();
		
		//是否有分组信息
		if(friendList.hasGroup()){
			Iterator<Group> it = friendList.getGroups().iterator();
			while(it.hasNext()){
				Group group = it.next();
				mGroupList.add(group);
				mMatchListMap.put(group.getGid(), new ArrayList<Match>());
			}
		}
		
		//遍历好友列表，查找对应的分组，更新到分组ID下，同时尝试匹配对应的通讯录好友
		Iterator<Friend> it = friendList.getFriends().iterator();
		while(it.hasNext()){
			Friend friend = it.next();
			Group group = findGroupById(friend.getGid());
			if(group==null) group = getDefaultGroup();
			Contact contact = Globals.CONTACT_MANAGER.findMostMatchedContact(friend);
			
			//添加到列表中
			Match match = new Match(friend, contact, MatchType.SYSTEM);
			List<Match> matches = mMatchListMap.get(group.getGid());
			matches.add(match);
		}
	}
	
	/**
	 * 返回所有分组的数量
	 * @return
	 */
	public int getGroupCount(){
		return mGroupList.size();
	}
	
	/**
	 * 返回一个分组
	 */
	public Group getGroup(int position){
		return mGroupList.get(position);
	}
	
	/**
	 * 获取指定组好友数量
	 * @param group
	 * @return
	 */
	public int getMatchCount(Group group){
		return mMatchListMap.get(group.getGid()).size();
	}
	
	/**
	 * 获取指定组指定位置好友列表
	 * @param group
	 * @return
	 */
	public Match getMatch(Group group,int position){
		return mMatchListMap.get(group.getGid()).get(position);
	}
	
	/**
	 * 获取当前所有成功已经匹配的好友列表
	 * @return
	 */
	public List<Match> getAreadyMatchedList(){
		List<Match> matched = new ArrayList<Match>();
		Iterator<Group> it = mGroupList.iterator();
		while(it.hasNext()){
			List<Match> matches = mMatchListMap.get(it.next().getGid());
			Iterator<Match> mit = matches.iterator();
			while(mit.hasNext()){
				Match match = mit.next();
				if(match.getContact()!=null){
					matched.add(match);
				}
			}
 		}
		return matched;
	}
	
	/**
	 * 根据组编号查找组，如果没有，就返回默认分组
	 * @param groupId
	 * @return
	 */
	public Group findGroupById(String groupId){
		Iterator<Group> it = mGroupList.iterator();
		while(it.hasNext()){
			Group group = it.next();
			if(group.getGid().equals(groupId)){
				return group;
			}
		}
		return null;
	}
	
	/**
	 * 根据好友ID查找匹配对象
	 * @param friendId
	 * @return
	 */
	public Match findMatchByFriendId(String friendId){
		Iterator<Group> it = mGroupList.iterator();
		while(it.hasNext()){
			List<Match> matches = mMatchListMap.get(it.next().getGid());
			Iterator<Match> mit = matches.iterator();
			while(mit.hasNext()){
				Match match = mit.next();
				if(match.getFriend().getFid().equals(friendId)){
					return match;
				}
			}
 		}
		return null;
	}
	
	/**
	 * 返回默认组，如果没有就新建默认组
	 * @return
	 */
	public Group getDefaultGroup(){
		Group defGroup = findGroupById("__default__");
		if(defGroup==null){
			defGroup = new Group("__default__", mDefaultGrouTitle);
			mGroupList.add(0, defGroup);
			mMatchListMap.put(defGroup.getGid(), new ArrayList<Match>());
		}
		return defGroup;
	}
	
	/**
	 * 添加一个匹配
	 * @param groupId
	 * @param match
	 */
	public void addMatch(String groupId, Match match){
		List<Match> matches = mMatchListMap.get(groupId);
		matches.add(match);
	}
	
	/**
	 * 批量添加匹配
	 * @param groupId
	 * @param matchList
	 */
	public void addMatches(String groupId, List<Match> matchList){
		List<Match> matches = mMatchListMap.get(groupId);
		matches.addAll(matchList);
	}
	
	/**
	 * 添加一个分组
	 * @param groupId
	 * @param title
	 */
	public void addGroup(String groupId, String title){
		mGroupList.add(new Group(groupId, title));
		mMatchListMap.put(groupId, new ArrayList<Match>());
	}
	
	/**
	 * 释放资源
	 */
	public void clear(){
		Iterator<Group> it = mGroupList.iterator();
		while(it.hasNext()){
			List<Match> matches = mMatchListMap.get(it.next().getGid());
			Iterator<Match> mit = matches.iterator();
			while(mit.hasNext()){
				Match match = mit.next();
				if(match.getFriend().getAvatar().getTag()!=null){
					Bitmap bm = (Bitmap) match.getFriend().getAvatar().getTag();
					bm.recycle();
					match.getFriend().getAvatar().setTag(null);
				}
				Contact contact = match.getContact();
				if(contact!=null && contact.getHead()!=null){
					contact.getHead().recycle();
					contact.setHead(null);
				}
			}
 		}
		
		mGroupList.clear();
		mMatchListMap.clear();
	}
	
	/**
	 * 重置，清空但不释放资源
	 */
	public void reset(){
		mGroupList.clear();
		mMatchListMap.clear();
	}
	
	/***
	 * 取消匹配
	 */
	public void cancelMatch(String friendId){
		Match match = findMatchByFriendId(friendId);
		if(match!=null){
			match.setContact(null);
		}
	}
	
	/**
	 * 更新一个匹配
	 * @param match
	 */
	public void updateMatch(Match match){
	}
	
}
